package com.ctlele.team.space.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class SharedPreferencesUtils {

    private SharedPreferencesUtils() {
    }

    /**
     * 设置SharedPreferences值
     *
     * @param context context
     * @param name    SharedPreferences的name
     * @param key     key
     * @param value   value
     * @return 是否成功
     */
    public static boolean set(Context context, String name, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sp.edit().putString(key, value).commit();
    }

    /**
     * 获取SharedPreferences值
     *
     * @param context context
     * @param name    SharedPreferences的name
     * @param key     key
     * @return SharedPreferences记录的值
     */
    public static String get(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }

    /**
     * 指定key是否为空
     *
     * @param context context
     * @param name    SharedPreferences的name
     * @param key     key
     * @return 是否为空
     */
    public static boolean isEmpty(Context context, String name, String key) {
        return TextUtils.isEmpty(get(context, name, key));
    }

    /**
     * 清空SharedPreferences的值
     *
     * @param context context
     * @param name    SharedPreferences的name
     * @param key     key
     * @return 是否成功
     */
    public static boolean clear(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sp.edit().remove(key).commit();
    }
}
